<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${mapperReference}">

    <resultMap id="BaseResultMap" type="${modelReference}">
        <#list columnList as data>
        <#if data.columnName=='ID' || data.columnName=='id'>
        <id column="ID" jdbcType="${data.columnType}" property="id"/>
        <#else>
        <result column="${data.columnName}" jdbcType="${data.columnType}" property="${data.varName}"/>
        </#if>
        </#list>
    </resultMap>

    <!--  字段列表  -->
    <sql id="base_column_list">
        <trim suffixOverrides=",">
            <#list columnList as data>
            ${data.columnName},
            </#list>
        </trim>
    </sql>

    <!--  新增字段列表  -->
    <sql id="insert_column_list">
        <trim suffixOverrides=",">
        <#list columnList as data>
            <if test="${data.varName} != null">${data.columnName},</if>
        </#list>
        </trim>
    </sql>

    <!--  新增value字段列表  -->
    <sql id="insert_value_list">
        <trim suffixOverrides=",">
        <#list columnList as data>
            <if test="${data.varName} != null"><#noparse>#{</#noparse>${data.varName},jdbcType=${data.columnType}<#noparse>}</#noparse>,</if>
        </#list>
        </trim>
    </sql>

    <!--  更新字段列表  -->
    <sql id="update_field_list">
        <trim suffixOverrides=",">
            <#list columnList as data>
            <#if data.varName=='id'>
                <#continue>
            </#if>
            <if test="${data.varName} != null">${data.columnName} = <#noparse>#{</#noparse>${data.varName},jdbcType=${data.columnType}<#noparse>}</#noparse>,</if>
            </#list>
        </trim>
    </sql>

    <!--  批量更新字段列表  -->
    <sql id="batch_update_field_list">
        <trim suffixOverrides=",">
            <#list columnList as data>
            <#if data.varName=='id'>
                <#continue>
            </#if>
            <if test="item.${data.varName} != null">${data.columnName} = <#noparse>#{</#noparse>item.${data.varName},jdbcType=${data.columnType}<#noparse>}</#noparse>,</if>
            </#list>
        </trim>
    </sql>


    <!-- 查询${tableInfo.tableComment}列表 -->
    <select id="query${tableInfo.className}List" resultMap="BaseResultMap" parameterType="java.util.Map">
        SELECT
        <include refid="base_column_list" />
        FROM ${tableInfo.tableName}
        <where>
        <#list columnList as data>
            <#if data.varName=='id' || data.excludeFromVO == true>
                <#continue>
            </#if>
            <if test="${data.varName} != null and ${data.varName} != ''">
                AND ${data.columnName} = <#noparse>#{</#noparse>${data.varName},jdbcType=${data.columnType}<#noparse>}</#noparse>
            </if>
        </#list>
        </where>
    </select>


    <!-- 根据ID查询${tableInfo.tableComment} -->
    <select id="get${tableInfo.className}ById" resultMap="BaseResultMap" parameterType="String">
        SELECT
        <include refid="base_column_list" />
        FROM ${tableInfo.tableName}
        <where>
            AND ID = <#noparse>#{</#noparse>id,jdbcType=VARCHAR<#noparse>}</#noparse>
        </where>
    </select>


    <!--  批量新增${tableInfo.tableComment}  -->
    <#if databaseName == "MySQL">
    <insert id="batchInsert${tableInfo.className}" parameterType="java.util.List">
        INSERT INTO ${tableInfo.tableName}
        <trim prefix=" (" suffix=") " suffixOverrides=",">
            <trim suffixOverrides=",">
                <#list columnList as data>
                ${data.columnName}<#if data_has_next>,</#if>
                </#list>
            </trim>
        </trim>
        values
        <foreach collection="list" item="item" index="index" separator=",">
            <trim prefix=" (" suffix=") " suffixOverrides=",">
                <#list columnList as data>
                <#noparse>#{</#noparse>item.${data.varName}<#noparse>}</#noparse><#if data_has_next>,</#if>
                </#list>
            </trim>
        </foreach>
    </insert>
    <#elseif databaseName == "Oracle" || databaseName == "DM DBMS">
    <insert id="batchInsert${tableInfo.className}" parameterType="java.util.List">
        INSERT ALL
        <foreach collection="list" item="item" index="index">
            INTO ${tableInfo.tableName}
            <trim prefix=" (" suffix=") " suffixOverrides=",">
                <#list columnList as data>
                ${data.columnName}<#if data_has_next>,</#if>
                </#list>
            </trim>
            VALUES
            <trim prefix=" (" suffix=") " suffixOverrides=",">
                <#list columnList as data>
                <#noparse>#{</#noparse>item.${data.varName}<#noparse>}</#noparse><#if data_has_next>,</#if>
                </#list>
            </trim>
        </foreach>
        SELECT 1 FROM DUAL
    </insert>
    <#else>
    </#if>


    <!-- 新增${tableInfo.tableComment} -->
    <insert id="insert${tableInfo.className}" keyColumn="ID" keyProperty="id" parameterType="${modelReference}" useGeneratedKeys="false">
        INSERT INTO ${tableInfo.tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <include refid="insert_column_list"/>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <include refid="insert_value_list"/>
        </trim>
    </insert>

    <!-- 根据ID修改${tableInfo.tableComment} -->
    <update id="update${tableInfo.className}ById" parameterType="${modelReference}">
        UPDATE ${tableInfo.tableName}
        <set>
            <include refid="update_field_list"/>
        </set>
        <#noparse>WHERE ID = #{id,jdbcType=VARCHAR}</#noparse>
    </update>


    <!-- 批量修改${tableInfo.tableComment} -->
    <update id="batchUpdate${tableInfo.className}" parameterType="java.util.List">
        <foreach collection="list" separator=";" item="item">
            UPDATE ${tableInfo.tableName}
            <set>
                <include refid="batch_update_field_list"/>
            </set>
            <#noparse>WHERE ID = #{item.id,jdbcType=VARCHAR}</#noparse>
        </foreach>
    </update>


    <!-- 删除${tableInfo.tableComment} -->
    <delete id="delete${tableInfo.className}ById" parameterType="String" >
        DELETE FROM ${tableInfo.tableName}
        <#noparse>WHERE ID = #{id,jdbcType=VARCHAR}</#noparse>
    </delete>


    <!-- 批量删除${tableInfo.tableComment} -->
    <delete id="delete${tableInfo.className}ByIdList" parameterType="java.util.List">
        DELETE FROM ${tableInfo.tableName}
        WHERE ID IN
        <foreach collection="list" item="id" open="(" separator="," close=")">
            <#noparse>#{id,jdbcType=VARCHAR}</#noparse>
        </foreach>
    </delete>

</mapper>
